package com.d.lib.ui.view.replybg;

import com.d.lib.ui.view.AttrUtils;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

public class ReplyBgView extends Component implements Component.EstimateSizeListener,
    Component.DrawTask {
  private final int LEFT = 0;
  private final int RIGHT = 1;
  private final int TOP = 2;
  private final int BOTTOM = 3;

  private int mWidth;
  private int mHeight;

  private Rect mRect;
  private RectFloat mRectF;
  private Paint mPaint;
  private Path mPathTrg; // Triangular path
  private int mGravity;
  private String stringGravity;
  private int mColorBg;
  private float mRectRadius;
  private float mOffset; // Triangle left offset
  private float mTrgHalfWidth; // Triangle bottom length/2
  private float mTrgHeight; // Triangle height

  public ReplyBgView(Context context) {
    this(context, null);
  }

  public ReplyBgView(Context context, AttrSet attrs) {
    this(context, attrs, 0);
  }

  public ReplyBgView(Context context, AttrSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initAttrs(context, attrs);
    init(context);
    addDrawTask(this::onDraw);
    setEstimateSizeListener(this::onEstimateSize);
  }


  private void initAttrs(Context context, AttrSet attrs) {
    stringGravity = AttrUtils.getStringFromAttr(attrs, "lib_ui_view_replybv_gravity", "top");
    switch (stringGravity) {
      case "left":
        mGravity = LEFT;
        break;
      case "right":
        mGravity = RIGHT;
        break;
      case "top":
        mGravity = TOP;
        break;
      case "bottom":
        mGravity = BOTTOM;
        break;
    }
    mColorBg = AttrUtils.getColorFromAttr(attrs, "lib_ui_view_replybv_color", Color.getIntColor(
        "#008577"));
    mRectRadius = AttrUtils.getDimensionFromAttr(attrs, "lib_ui_view_replybv_radius",
        AttrHelper.vp2px(3, context));
    mOffset = AttrUtils.getDimensionFromAttr(attrs, "lib_ui_view_replybv_offset",
        AttrHelper.vp2px(6.5f, context));
    mTrgHalfWidth = AttrUtils.getDimensionFromAttr(attrs, "lib_ui_view_replybv_trgWidth",
        AttrHelper.vp2px(6, context)) / 2;
    mTrgHeight = AttrUtils.getDimensionFromAttr(attrs, "lib_ui_view_replybv_trgHeight",
        AttrHelper.vp2px(5, context));
  }

  private void init(@SuppressWarnings("unused") Context context) {
    mPathTrg = new Path();
    mRect = new Rect();
    mRectF = new RectFloat();
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setColor(new Color(mColorBg));
  }

  /**
   * 设置背景颜色
   */
    public void setmColorBg(int color) {
    mColorBg = color;
  }

  /**
   * 设置三角位置
   */
    public void setStringGravity(String gravity) {
      stringGravity = gravity;
  }

 /**
   * 设置偏移
   */
    public void setmOffset(float offset) {
      mOffset = offset;
  }

 /**
   * 设置圆角
   */
    public void setmRectRadius(float rectRadius) {
      mRectRadius = rectRadius;
  }

 /**
   * 设置高度
   */
    public void setmTrgHeight(float height) {
      mTrgHeight = height;
  }

 /**
   * 设置宽度
   */
    public void setmTrgHalfWidth(float width) {
      mTrgHalfWidth = width;
  }

  @Override
  public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {
    //  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mWidth = MeasureSpec.getSize(widthMeasureSpec);
    mHeight = MeasureSpec.getSize(heightMeasureSpec);
    setEstimatedSize(mWidth, mHeight);
    return false;
  }

  @Override
  public void onDraw(Component component, Canvas canvas) {
    //super.onDraw(canvas);
    switch (mGravity) {
      case LEFT:
        mPathTrg.moveTo(mTrgHeight, mOffset > 0 ? mOffset : mHeight + mOffset - mTrgHalfWidth * 2);
        mPathTrg.lineTo(0, mOffset > 0 ? mOffset + mTrgHalfWidth :
            mHeight + mOffset - mTrgHalfWidth);
        mPathTrg.lineTo(mTrgHeight, mOffset > 0 ? mOffset + mTrgHalfWidth * 2 : mHeight + mOffset);
        mPathTrg.close();
        canvas.drawPath(mPathTrg, mPaint);

        mRect.set((int) mTrgHeight, 0, mWidth, mHeight);
        mRectF.modify(mRect);
        canvas.drawRoundRect(mRectF, mRectRadius, mRectRadius, mPaint);
        break;

      case RIGHT:
        mPathTrg.moveTo(mWidth - mTrgHeight, mOffset > 0 ? mOffset :
            mHeight + mOffset - mTrgHalfWidth * 2);
        mPathTrg.lineTo(mWidth, mOffset > 0 ? mOffset + mTrgHalfWidth :
            mHeight + mOffset - mTrgHalfWidth);
        mPathTrg.lineTo(mWidth - mTrgHeight, mOffset > 0 ? mOffset + mTrgHalfWidth * 2 :
            mHeight + mOffset);
        mPathTrg.close();
        canvas.drawPath(mPathTrg, mPaint);

        mRect.set(0, 0, (int) (mWidth - mTrgHeight), mHeight);
        mRectF.modify(mRect);
        canvas.drawRoundRect(mRectF, mRectRadius, mRectRadius, mPaint);
        break;

      case TOP:
        mPathTrg.moveTo(mOffset > 0 ? mOffset : mWidth + mOffset - mTrgHalfWidth * 2, mTrgHeight);
        mPathTrg.lineTo(mOffset > 0 ? mOffset + mTrgHalfWidth : mWidth + mOffset - mTrgHalfWidth,
            0);
        mPathTrg.lineTo(mOffset > 0 ? mOffset + mTrgHalfWidth * 2 : mWidth + mOffset, mTrgHeight);
        mPathTrg.close();
        canvas.drawPath(mPathTrg, mPaint);

        mRect.set(0, (int) mTrgHeight, mWidth, mHeight);
        mRectF.modify(mRect);
        canvas.drawRoundRect(mRectF, mRectRadius, mRectRadius, mPaint);
        break;

      case BOTTOM:
        mPathTrg.moveTo(mOffset > 0 ? mOffset : mWidth + mOffset - mTrgHalfWidth * 2,
            mHeight - mTrgHeight);
        mPathTrg.lineTo(mOffset > 0 ? mOffset + mTrgHalfWidth : mWidth + mOffset - mTrgHalfWidth,
            mHeight);
        mPathTrg.lineTo(mOffset > 0 ? mOffset + mTrgHalfWidth * 2 : mWidth + mOffset,
            mHeight - mTrgHeight);
        mPathTrg.close();
        canvas.drawPath(mPathTrg, mPaint);

        mRect.set(0, 0, mWidth, (int) (mHeight - mTrgHeight));
        mRectF.modify(mRect);
        canvas.drawRoundRect(mRectF, mRectRadius, mRectRadius, mPaint);
        break;
    }

  }
}